import pygame, sys

from pygame.locals import *
from Block import *
from World import World
from Constants import *
from Images import *
from Cursor import Cursor
from Puzzle import PuzzleHandler
from Puzzle import Puzzle
from Person import makePerson
from SideBar import SideBar
from House import House
#from Animation import Animation

animator = None

class Game:
    def __init__(self):
        self.window = pygame.display.set_mode((800,600))
        pygame.display.set_caption('CuteGod')
        
        self.screen = pygame.display.get_surface()
        
        self.background = pygame.Surface(self.screen.get_size()).convert()
        self.background.fill((50,50,50))

        self.keyHandler = {K_ESCAPE : lambda : sys.exit(0)}
        self.keyHandler[K_SPACE] = lambda : self.newMap()

        self.blocks = {}
        self.houseBlocks = {}
        self.shadows = {}
        self.actors = {}
        
        self.loading()
        
        self.newMap()
        actor = makePerson('Character Boy', self.actors, (3, 3))
        
        self.map.addActor(actor, (3, 3))

        puzzle = Puzzle(House(self.houseBlocks), actor)
        
        self.puzzleHandler = PuzzleHandler()
        self.puzzleHandler.addPuzzle(puzzle)
        
        self.sideBar = SideBar()
        self.sideBar.addPuzzle(puzzle)
        
        self.cursor = Cursor(self.map, self.puzzleHandler,self.sideBar)
        self.cursor.mapKeys(self.keyHandler)
        
        #animator = Animation(self)
        
        self.render()
    
    def mainLoop(self):
        self.render()
        while True:
            self.input(pygame.event.get())
    
    def loading(self):
        # Blocks      
        cacheImage(self.blocks, 'Grass Block')
        cacheImage(self.blocks, 'Water Block')
        cacheImage(self.blocks, 'Stone Block')
        #cacheImage(self.blocks, 'Wood Block')
        cacheImage(self.blocks, 'Dirt Block')
        
        #Shadows      cacheImage(self.shadows, 'Shadow South East')
        cacheImage(self.shadows, 'Shadow South')
        cacheImage(self.shadows, 'Shadow South West')
        cacheImage(self.shadows, 'Shadow East')
        cacheImage(self.shadows, 'Shadow West')
        cacheImage(self.shadows, 'Shadow North East')
        cacheImage(self.shadows, 'Shadow North')
        cacheImage(self.shadows, 'Shadow North West')
        cacheImage(self.shadows, 'Shadow Side West')
        
        #Charactors
        cacheImage(self.actors, 'Character Boy')
        cacheImage(self.actors, 'Character Cat Girl')
        cacheImage(self.actors, 'Character Horn Girl')
        cacheImage(self.actors, 'Character Pink Girl')
        cacheImage(self.actors, 'Character Princess Girl')
        
        #House Blocks
        cacheImage(self.houseBlocks, 'Roof East')
        cacheImage(self.houseBlocks, 'Roof West')
        cacheImage(self.houseBlocks, 'Door Tall Closed')
        cacheImage(self.houseBlocks, 'Door Tall Open')
        cacheImage(self.houseBlocks, 'Brown Block')
        cacheImage(self.houseBlocks, 'Window Tall')
        

    def newMap(self):
        self.map = World(self.blocks, self.shadows, MAP_X, MAP_Y, MAP_Z)
    
    def render(self):
        self.screen.blit(self.background, (0,0))
        self.map.render(self.screen)
        self.sideBar.render(self.screen)
        pygame.display.flip()
    
    def input(self,events):
        for event in events:
            if event.type == QUIT:
                print 'Goodbye!'
                sys.exit(0)
            elif event.type == KEYDOWN:
                try:
                    self.keyHandler[event.key]()
                    self.render()
                except KeyError:
                    print 'key not bound'
            elif event.type == MOUSEMOTION and USE_MOUSE:
                if self.cursor.mouseMoved(event.pos):
                    self.render()
            elif event.type == MOUSEBUTTONDOWN and USE_MOUSE:
                self.cursor.yoink()
                self.render()
game = Game().mainLoop()